package reflection.dynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxyer implements InvocationHandler{
	private Object proxyObj;

	private DynamicProxyer(Object proxy) {
		this.proxyObj = proxy;
	}
	
	public static Object factory(Object obj){
		Class cls = obj.getClass();
		return Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(),new DynamicProxyer(obj));
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		preInvoke();
		Object returnObject = method.invoke(proxyObj, args);
		postInvoke();
		return returnObject;
	}
	private void preInvoke(){
		System.out.println("preInvoke!");
	}
	
	private void postInvoke(){
		System.out.println("postInvoke!");
	}
}
